package lib.XML;

import lib.Base.Settings;
import lib.Common.Cast;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

public class SettingsContentHandler implements ContentHandler {
	/**
	 * Class to handle XML content for settings
	 */
	private String currentValue;
	private Settings settings;

	public void characters(char[] ch, int start, int length)
			throws SAXException {
		currentValue = new String(ch, start, length);
	}

	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
		if (localName.equals("settings")) {
			settings = new Settings();
		}
	}

	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (localName.equals("visible")) {
			settings.setPVisible(Cast.StringToInt(currentValue));
		} else if (localName.equals("guestbook")) {
			settings.setPGuestbook(Cast.StringToInt(currentValue));
		} else if (localName.equals("searchable")) {
			settings.setPSearchable(Cast.StringToBoolean(currentValue));
		} else if (localName.equals("silent")) {
			settings.setPSilent(Cast.StringToBoolean(currentValue));
		} else if (localName.equals("visibleFriendlist")) {
			settings.setPFriendlist(Cast.StringToInt(currentValue));
		} else if (localName.equals("allowed")) {
			settings.setCAllowed(Cast.StringToInt(currentValue));
		} else if (localName.equals("user")) {
			settings.addBlockedUser(currentValue);
		} else if (localName.equals("allowFinvite")) {
			settings.setUAllowedFInvites(Cast.StringToBoolean(currentValue));
		} else if (localName.equals("personalData")) {
			settings.setDPersonalData(Cast.StringToInt(currentValue));
		} else if (localName.equals("careerData")) {
			settings.setDCareerData(Cast.StringToInt(currentValue));
		} else if (localName.equals("hobbys")) {
			settings.setDHobbys(Cast.StringToInt(currentValue));
		} else if (localName.equals("allowInvites")) {
			settings.setGAllowGInvites(Cast.StringToBoolean(currentValue));
		} else if (localName.equals("send")) {
			settings.setLTSend(Cast.StringToBoolean(currentValue));
		}
	}

	public Settings getSettings() {
		return settings;
	}

	public void endDocument() throws SAXException {
	}

	public void endPrefixMapping(String prefix) throws SAXException {
	}

	public void ignorableWhitespace(char[] ch, int start, int length)
			throws SAXException {
	}

	public void processingInstruction(String target, String data)
			throws SAXException {
	}

	public void setDocumentLocator(Locator locator) {
	}

	public void skippedEntity(String name) throws SAXException {
	}

	public void startDocument() throws SAXException {
	}

	public void startPrefixMapping(String prefix, String uri)
			throws SAXException {
	}
}
